<?php

class Silk_Application_Subsystem_Messaging implements FP_Application_Subsystem_Interface {

    protected $_config = null;

    protected static $Hooks = array();

    public function init($pConfig=null){
	$this->_config = $pConfig;

	$this->initHooks();
    }

    protected function initHooks(){
	self::CreateHooks(array(
	    'object-pre-save',
	    'object-post-save',
	    'object-pre-create',
	    'object-post-create',
	    'object-pre-edit',
	    'object-post-edit',
	    'object-pre-delete',
	    'object-post-delete',
	    'object-pre-view',
	    'object-pre-render',
	));

    }

    public static function TriggerHook($pName, $pArgs){
	logdebug("Silk_Application_Subsystem_Messaging.TriggerHook: Triggering hook [$pName]");
	if (array_key_exists($pName, self::$Hooks)){
	    $callbacks = self::$Hooks[$pName];
	    Silk::debug("Silk_Application_Subsystem_Messaging.TriggerHook: Calling " . count($callbacks) . " callbacks for hook- [$pName]");
	    foreach ($callbacks as $callback){
// 		Silk::debug("Silk_Application_Subsystem_Messaging.TriggerHook: Calling " . logobject($callback));
		call_user_func($callback, $pArgs);
	    }
	}
    }

    public static function CreateHooks(array $pHookNames){
	foreach ($pHookNames as $hookname)
	    self::CreateHook($hookname);
    }

    public static function CreateHook($pName){
	if (!array_key_exists($pName, self::$Hooks)){
	    self::$Hooks[$pName] = array();
	    return true;
	} else
	    throw new Silk_Exception("Hook already exists with name [$pName]");
    }

    public static function RegisterCallback($pFor, $pCallback){
	if (array_key_exists($pFor, self::$Hooks)){
	    Silk::debug("Silk_Application_Subsystem_Messaging.RegisterCallback: Registration for hook [$pFor]");
	    self::$Hooks[$pFor][] = $pCallback;
	    return true;
	}

	return false;
    }

}

?>